using System;
using System.IO;
using System.Threading.Tasks;
using Admin3000.Backend.Api.Params;
using Admin3000.Backend.Api.Entity;
using Admin3000.Backend.Api.Repository;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Admin3000.Backend.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Configuration;


namespace Admin3000.Backend.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[Controller]")]
    public class QRcodeController : ControllerBase
    {

        private IConfiguration _configuration;

        private readonly IWebHostEnvironment _iWebHostEnvironment;

        private IRepository<QRcode> _QRcodeInfoIRepository;

        private dynamic PicturePath;

         private TokenParameter _tokenParameter;

        public QRcodeController(
             IConfiguration configuration,
            IWebHostEnvironment iWebHostEnvironment,
            IRepository<QRcode> QRcodeInfoIRepository
            )
        {
             _configuration = configuration;
            _iWebHostEnvironment = iWebHostEnvironment;
            _QRcodeInfoIRepository = QRcodeInfoIRepository;
            _tokenParameter =
                _configuration
                    .GetSection("TokenParameter")
                    .Get<TokenParameter>();
        }
          [HttpGet]
        public string Get([FromQuery] Pager  pager)
        {
               
            var QRcode = _QRcodeInfoIRepository.Table;
            var res=new
            {
                Code = 1000,
                Data = QRcode,
                Msg = "请求成功"
            };
            return JsonHelper.Serialize(res);
            
        }

        public ActionResult Index()
        {
            PicturePath = _iWebHostEnvironment.ContentRootPath;
            return null;
        }

        [HttpPost, Route("files")]
        public async Task<IActionResult> UploadFiles()
        {
            Index();
            string filesPath = "";
            var files = Request.Form.Files;
            //相对路径
            string tempPath = DateTime.Now.ToString("yyyyMMdd");

            string physicPath = PicturePath + "\\files\\" + tempPath;


            if (!Directory.Exists(physicPath))
            {
                Directory.CreateDirectory(physicPath);
            }
            
            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    // //文件名字
                    string filesName = DateTime.Now.ToFileTime() + Path.GetExtension(formFile.FileName);
                    //绝对路径
                    string fileFullPath = Path.Combine(physicPath, filesName);
                    using (var stream = new FileStream(fileFullPath, FileMode.Create))
                    {
                        await formFile.CopyToAsync(stream);//保存文件
                    }
                    filesPath += tempPath + '\\' + filesName ;
                    var picturesql = new QRcode
                    {
                        PictureName = filesName,
                        PicturePath = filesPath
                    };
                    await _QRcodeInfoIRepository.InsertAsync(picturesql);
                }
            }
            filesPath = filesPath.TrimEnd(',');
            return Ok(new { filesPath });


        }
       
    }
}